import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jd/config/config.dart';
import 'package:flutter_jd/config/global.dart';
import 'package:flutter_jd/pages/launch/view.dart';
import 'package:flutter_jd/routes/router.dart';
import 'package:flutter_jd/service/global_service.dart';
import 'package:flutter_jd/theme/app_theme.dart';
import 'package:flutter_jd/tools/utils/utils.dart';
import 'package:flutter_jd/ui/widgets/body/tab_navigator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/route_manager.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';

Future<void> main() async {
  await initServices();
  runApp(MyApp());
}

// 初始化
Future<void> initServices() async {
  print('starting services ...');
  await Get.putAsync(() => GlobalConfigService().init());
  await Global.init();
  print('All services started...');
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final botToastBuilder = BotToastInit();
    final int inte = Utils.getStartTimeDifference();

    return ScreenUtilInit(
      designSize: Size(375, 812),
      builder: () => GetMaterialApp(
        title: Config.APP_NAME,
        getPages: AppPages.pages,
        home: inte >= Config.LAUNCH_INTERVAL ? LaunchPage() : TabNavigator(),
        builder: (context, widget) {
          widget = botToastBuilder(context, widget);
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget,
          );
        },
        enableLog: false,
        theme: AppTheme.defaultTheme,
        darkTheme: AppTheme.darkTheme,
        themeMode: ThemeMode.system,
        routingCallback: (routing) {},
        debugShowCheckedModeBanner: false,
        defaultTransition: Transition.native,
        locale: Locale('zh', 'CH'),
        // logWriterCallback: Logger.write,
        localizationsDelegates: [
          GlobalCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('zh', 'CH'),
          const Locale('en', 'US'),
        ],
      ),
    );
  }
}
